package webservice.impl;




import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import webservice.HrService;

import com.zk.entity.A401;
import com.zk.entity.A403;
import com.zk.entity.A404;
import com.zk.entity.K_month;
import com.zk.entity.Oa;
import com.zk.entity.Org;
import com.zk.entity.Person;
import com.zk.service.EmployeeAskForLeaveService;
import com.zk.service.EmployeeRegularService;
import com.zk.service.K_monthService;
import com.zk.service.OrgService;
import com.zk.service.PersonService;
import com.zk.service.PersonnelChangeService;
import com.zk.util.Request;
import com.zk.util.Response;
import com.zk.util.Util;
import com.zk.util.WebServiceConstants;
public class HrServiceImpl implements HrService {

	@Autowired
	private OrgService orgService;
	
/*	@Autowired
	private PersonService userService;*/
	
	@Autowired
	private K_monthService k_monthService;
	
	@Autowired
	private PersonnelChangeService personnelChangeService;
	
	@Autowired
	private EmployeeAskForLeaveService employeeAskForLeaveService;
	
	@Autowired
	private EmployeeRegularService employeeRegularService;

	@Override
	public String BPHC_HR_ORGADD(String request) {
	    Request r = new Request(request);
	    Object object = null;
	    Date date = null;
		Org org = new Org();
		org = (Org) r.bindRequestData(org);
		SimpleDateFormat sdf =   new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			date = sdf.parse(r.getHeader().getSendTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		org.setDate(date);
		org.setUpdatetime(date);
	    System.out.println(request);
	    orgService.addOrg(org);
		
	    return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, object).getXmlStr();
	}

	@Override
	public String BPHC_HR_ORGEDIT(String request) {
		
		Request r = new Request(request);
		Org org = new Org();
		Object object = null;
		org = (Org) r.bindRequestData(org);
		Date date = null;
		SimpleDateFormat sdf =   new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			date = sdf.parse(r.getHeader().getSendTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		org.setUpdatetime(date);
	    System.out.println(request);
	    orgService.updateOrganization(org);
		
	    return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, object).getXmlStr();
	}

	@Override
	public String BPHC_HR_ORGDELETE(String request) {
		Object object = null;
		Request r = new Request(request);
		Org org = new Org();
		org = (Org) r.bindRequestData(org);
		orgService.deleteOrganization(org);
		return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, object).getXmlStr();
	}

	@Override
	public String BPHC_HR_ORGDELETELOGIC(String request) {
		Object object = null;
		Request r = new Request(request);
		Org org = new Org();
		org = (Org) r.bindRequestData(org);
		org.setUpdatetime(new Date());
		orgService.deleteOrganizationLogic(org);
		return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, object).getXmlStr();
	}

	/**
	 * 获取人员信息
	 */
	@Override
	public String BPHC_OA_GETUSERINFOBYTIME(String request) {
		/*Request r = new Request(request);
		Oa oa = new Oa();
		oa = (Oa) r.bindRequestData(oa);
		int timeNum = Integer.valueOf(oa.getTime());
		List<String> list=new ArrayList<String>();
		if(null != oa.getDeptid()){
			String[] dps = oa.getDeptid().split(",");
			list = java.util.Arrays.asList(dps);
		}

		Date currDate = new Date();
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String endtime = sdf.format(currDate);

	    Date afterDate = new Date(currDate.getTime() - timeNum*60*1000);
	    String starttime = sdf.format(afterDate);
	    
	    List<Person> p = userService.getPerson(starttime, endtime, list);
	    List<com.zk.dto.Person> plist = new ArrayList<com.zk.dto.Person>();
	    if(null != p && !p.isEmpty()){
	    	for (Person person : p) {
	    		com.zk.dto.Person pdto = new com.zk.dto.Person();
	    		String JsonContext = new Util().ReadFile(HrServiceImpl.class.getClassLoader().getResource("trans.json").getPath());   
	            JSONObject jb = JSONObject.fromObject(JsonContext);
	            JSONObject bm_zwj = JSONObject.fromObject(jb.getString("bm_zwj"));
	            JSONObject e01 = JSONObject.fromObject(jb.getString("e01"));
	            JSONObject a01 = JSONObject.fromObject(jb.getString("a01"));
	            JSONObject bm_xl = JSONObject.fromObject(jb.getString("bm_xl"));
	            JSONObject bm_bxjnd = JSONObject.fromObject(jb.getString("bm_bxjnd"));
	            JSONObject bm_ae = JSONObject.fromObject(jb.getString("bm_ae"));
	            JSONObject bm_at = JSONObject.fromObject(jb.getString("bm_at"));
	            if(null != person.getA1020() && !"".equals(person.getA1020())){
	            	String temp = bm_zwj.getString(person.getA1020());
	            	person.setA1020(temp);
	            }
	            if(null != person.getA0113() && !"".equals(person.getA0113())){
	            	String temp = person.getA0113();
	            	System.out.println("等级:"+temp);
	            	if("001".equals(temp) || "019".equals(temp)){
	            		pdto.setSeclevel("99");
	            	}
	            	if("002".equals(temp) || "020".equals(temp) || "021".equals(temp)){
	            		pdto.setSeclevel("90");
	            	}
	            	if("003".equals(temp) || "022".equals(temp)){
	            		pdto.setSeclevel("85");
	            	}
	            	if("004".equals(temp)){
	            		pdto.setSeclevel("70");
	            	}
	            	if("005".equals(temp)){
	            		pdto.setSeclevel("60");
	            	}
	            	if("006".equals(temp) || "007".equals(temp) || "008".equals(temp) || "023".equals(temp)){
	            		pdto.setSeclevel("50");
	            	}
	            	if("009".equals(temp) || "010".equals(temp) || "011".equals(temp) || "012".equals(temp) || "013".equals(temp) || "014".equals(temp)){
	            		pdto.setSeclevel("30");
	            	}
	            	if("015".equals(temp) || "016".equals(temp) || "017".equals(temp) || "018".equals(temp) || "024".equals(temp) || "025".equals(temp)){
	            		pdto.setSeclevel("10");
	            	}
	            	person.setA0113(temp);
	            }
	            if(null != person.getA01103() && !"".equals(person.getA01103())){
	            	String temp = bm_xl.getString(person.getA01103());
	            	person.setA01103(temp);
	            }
	            if(null != person.getA01105() && !"".equals(person.getA01105())){
	            	String temp = bm_bxjnd.getString(person.getA01105());
	            	person.setA01105(temp);
	            }
	            if(null != person.getA0121() && !"".equals(person.getA0121())){
	            	String temp = bm_ae.getString(person.getA0121());
	            	person.setA0121(temp);
	            }
	            if(null != person.getA01577() && !"".equals(person.getA01577())){
	            	String temp = bm_at.getString(person.getA01577());
	            	person.setA01577(temp);
	            }
	            
	            if(null != person.getA01728() && !"".equals(person.getA01728())){
	            	if("1".equals(person.getA01728()) ||  "5".equals(person.getA01728()) || "6".equals(person.getA01728()) || "7".equals(person.getA01728()) || "10".equals(person.getA01728() )){
	            		person.setA01728("1");
	            		pdto.setA01728("1");
	            	}
	            	if("2".equals(person.getA01728()) || "4".equals(person.getA01728())){
	            		person.setA01728("0");
	            		pdto.setA01728("0");
	            	}
	            	if("3".equals(person.getA01728())){
	            		person.setA01728("2");
	            		pdto.setA01728("2");
	            	}
	            	if("8".equals(person.getA01728())){
	            		person.setA01728("5");
	            		pdto.setA01728("5");
	            	}
	            	if("9".equals(person.getA01728())){
	            		person.setA01728("6");
	            		pdto.setA01728("6");
	            	}
	            	if("11".equals(person.getA01728())){
	            		person.setA01728("7");
	            		pdto.setA01728("7");
	            	}
	            }
	            
	            if(null != person.getA0107() && !"".equals(person.getA0107())){
	            	
	            	if("1".equals(person.getA0107())){
	            		person.setA0107("0");
	            		pdto.setA0107("0");
	            	}
	            	if("2".equals(person.getA0107())){
	            		person.setA0107("1");
	            		pdto.setA0107("1");
	            	}
	            }
	            
	            if(null != person.getA0127() && !"".equals(person.getA0127())){
	            	
	            	if("1".equals(person.getA0127())){
	            		person.setA0127("0");
	            	}
	            	if("2".equals(person.getA0127())){
	            		person.setA0127("1");
	            	}
	            	if("4".equals(person.getA0127())){
	            		person.setA0127("2");
	            	}
	            }
	            if(null != person.getA0190() && !"".equals(person.getA0190())){
	            	pdto.setA0190(person.getA0190());
	            }
	            if(null != person.getA0101() && !"".equals(person.getA0101())){
	            	pdto.setA0101(person.getA0101());
	            }
	            if(null != person.getA01666() && !"".equals(person.getA01666())){
	            	pdto.setA01666(person.getA01666());
	            }
	            if(null != person.getJ01_dept_id() && !"".equals(person.getJ01_dept_id())){
	            	pdto.setJ01_dept_id(person.getJ01_dept_id());
	            }
	            if(null != person.getJ01_e0101() && !"".equals(person.getJ01_e0101())){
	            	String temp = e01.getString(person.getJ01_e0101());
	            	pdto.setJ01_e0101(temp);
	            }
	            if(null != person.getOne_per() && !"".equals(person.getOne_per())){
	            	String temp = a01.getString(person.getOne_per());
	            	pdto.setManagerid(temp);
	            }
	            if(null != person.getA01274() && !"".equals(person.getA01274())){
	            	pdto.setA01274(person.getA01274());
	            }
	            if(null != person.getEmail() && !"".equals(person.getEmail())){
	            	pdto.setEmail(person.getEmail());
	            }
	            plist.add(pdto);
			}
	    	
	    	
	    	
	    	return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, plist).getXmlStr();
	    }*/
	   /* return new Response(r.getHeader(), "无更新内容！").getXmlStr();*/
		return null;
	}

	/**
	 * 获取部门信息
	 */
	@Override
	public String BPHC_OA_GETORGINFOBYTIME(String request) {
		
		Request r = new Request(request);
		Oa oa = new Oa();
		oa = (Oa) r.bindRequestData(oa);
		int timeNum = Integer.valueOf(oa.getTime());
		List<String> list=new ArrayList<String>();
		if(null != oa.getDeptid()){
			String[] dps = oa.getDeptid().split(",");
			list = java.util.Arrays.asList(dps);
		}

		System.out.println(list.size());
		Date currDate = new Date();
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String endtime = sdf.format(currDate);
	    Date afterDate = new Date(currDate.getTime() - timeNum*60*1000);
	    String starttime = sdf.format(afterDate);
	    List<com.zk.dto.Org> orgdtoList = new ArrayList<com.zk.dto.Org>();
	    List<Org> orgList = orgService.getOrganization(starttime, endtime, list);
	    if(null != orgList && !orgList.isEmpty()){
	    	for (Org org : orgList) {
	    		com.zk.dto.Org orgdto = new com.zk.dto.Org();
	    		if(null != org.getB01043() && !"".equals(org.getB01043())){
	    			orgdto.setJg(org.getB01043());
	    		}
	    		if(null != org.getDept_code() && !"".equals(org.getDept_code())){
	    			orgdto.setDept_code(org.getDept_code());
	    		}
	    		if(null != org.getContent() && !"".equals(org.getContent())){
	    			orgdto.setContent(org.getContent());
	    		}
	    		if(null != org.getParentcode() && !"".equals(org.getParentcode())){
	    			orgdto.setParentcode(org.getParentcode());
	    		}
	    		if(null != org.getParent_id() && !"".equals(org.getParent_id())){
	    			orgdto.setParent_id(org.getParent_id());
	    		}
	    		if(null != org.getB01deleted() && !"".equals(org.getB01deleted())){
	    			orgdto.setB01deleted(org.getB01deleted());
	    		}
	    		orgdtoList.add(orgdto);
	    	}
	    	
	    	return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, orgdtoList).getXmlStr();
	    }
	    return new Response(r.getHeader(), "无更新内容！").getXmlStr();
	}

	/**
	 * 新增人员
	 */
	@Override
	public String BPHC_HR_PERSONADD(String request) {
		Object object = null;
		Request r = new Request(request);
		Person person = new Person();
		person = (Person) r.bindRequestData(person);
		Date date = null;
		SimpleDateFormat sdf =   new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			date = sdf.parse(r.getHeader().getSendTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
        person.setDate(date);
        person.setUpdatetime(date);
		//userService.addPerson(person);
		return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, object).getXmlStr();
	}

	/**
	 * 删除人员（物理）
	 */
	@Override
	public String BPHC_HR_PERSONDELETE(String request) {
		Object object = null;
		Request r = new Request(request);
		Person person = new Person();
		person = (Person) r.bindRequestData(person);
		//userService.deletePerson(person);
		return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, object).getXmlStr();
	}

	/**
	 * 删除人员（逻辑）
	 */
	@Override
	public String BPHC_HR_PERSONDELETELOGIC(String request) {
		Object object = null;
		Request r = new Request(request);
		Person person = new Person();
		person = (Person) r.bindRequestData(person);
		person.setUpdatetime(new Date());
		//userService.deletePersonLogic(person);
		return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, object).getXmlStr();
	}

	/**
	 * 修改人员
	 */
	@Override
	public String BPHC_HR_PERSONEDIT(String request) {
		Object object = null;
		Request r = new Request(request);
		Person person = new Person();
		person = (Person) r.bindRequestData(person);
		Date date = null;
		SimpleDateFormat sdf =   new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			date = sdf.parse(r.getHeader().getSendTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		person.setUpdatetime(date);
		//userService.updatePerson(person);
		return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, object).getXmlStr();
	}

	/**
	 * 考勤上报
	 */
	@Override
	public String BPHC_OA_ATTENDANCEREPORT(String request) {
		
		Request r = new Request(request);
	    
	    Object object = null;
	    K_month k_month = new K_month();
	    k_month = (K_month) r.bindRequestData(k_month);
	    Date date = null;
	    SimpleDateFormat sdf =   new SimpleDateFormat("yyyyMMddHHmmss");
	    try {
			date = sdf.parse(r.getHeader().getSendTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
	    k_month.setAddtime(date);
		k_monthService.addK_monthInfo(k_month);
	    System.out.println(request);
		
	    return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, object).getXmlStr();
	}

	/**
	 * 员工请假
	 */
	@Override
	public String BPHC_OA_EMPLOYEEASKFORLEAVE(String request) {
		Request r = new Request(request);
	    
	    Object object = null;
	    A404 a404 = new A404();
	    a404 = (A404) r.bindRequestData(a404);
	    Date date = null;
	    SimpleDateFormat sdf =   new SimpleDateFormat("yyyyMMddHHmmss");
	    try {
			date = sdf.parse(r.getHeader().getSendTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
	    a404.setAddtime(date);
	    employeeAskForLeaveService.addEmployeeAskForLeave(a404);
		
	    return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, object).getXmlStr();
	}

	/**
	 * 员工离职
	 */
	@Override
	public String BPHC_OA_EMPLOYEEDIMISSION(String request) {
		return null;
	}

	/**
	 * 员工转正
	 */
	@Override
	public String BPHC_OA_EMPLOYEEREGULAR(String request) {
		Request r = new Request(request);
	    
	    Object object = null;
	    A403 a403 = new A403();
	    a403 = (A403) r.bindRequestData(a403);
	    Date date = null;
	    SimpleDateFormat sdf =   new SimpleDateFormat("yyyyMMddHHmmss");
	    try {
			date = sdf.parse(r.getHeader().getSendTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
	    a403.setAddtime(date);

	    employeeRegularService.addEmployeeRegular(a403);
	    return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, object).getXmlStr();
	}

	/**
	 * 人事变动
	 */
	@Override
	public String BPHC_OA_PERSONNELCHANGE(String request) {
		Request r = new Request(request);
	    
	    Object object = null;
	    A401 a401 = new A401();
	    a401 = (A401) r.bindRequestData(a401);
	    Date date = null;
	    SimpleDateFormat sdf =   new SimpleDateFormat("yyyyMMddHHmmss");
	    try {
			date = sdf.parse(r.getHeader().getSendTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
	    a401.setAddtime(date);
	    a401.setAddtime(date);
	    personnelChangeService.addPersonnelChange(a401);
		
	    return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, object).getXmlStr();
	}

	/**
	 * 部门移动
	 */
	@Override
	public String BPHC_HR_ORGMOVE(String request) {
		Object object = null;
		Request r = new Request(request);
		Org org = new Org();
		org = (Org) r.bindRequestData(org);
		org.setUpdatetime(new Date());
		orgService.organizationMove(org);
		return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, object).getXmlStr();
	}

	/**
	 * 先到先得
	 */
	@Override
	public String BPHC_HOMS_FIRSTCOMEFIRSTSERVED(String request) {
		// TODO Auto-generated method stub
		return null;
	}


	/**
	 * 意向登记
	 */
	@Override
	public String BPHC_HOMS_INTENTIONREGISTER(String request) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 实时配租
	 */
	@Override
	public String BPHC_HOMS_REALTIMERENT(String request) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 房源空置和维修状态转换
	 */
	@Override
	public String BPHC_HOMS_HOUSINGVACANTANDSTATUSTRANS(String request) {
		// TODO Auto-generated method stub
		return null;
	}

}
